import api from "../API/index";
import getDate from "../utils/getDate";
export default {
  state: {
    userInfo: JSON.parse(localStorage.getItem("userInfo")) || {}, //用户信息
    homeData: JSON.parse(localStorage.getItem("homeData")) || {},
    mallData: JSON.parse(localStorage.getItem("mallData")) || [],
  },
  mutations: {
    //设置用户信息
    setUserInfo(state, user) {
      state.userInfo = user;
    },
    //设置首页图表数据信息
    setHomeData(state, data) {
      state.homeData = data;
    },
    //设置商品管理页面数据信息
    setMallData(state, data) {
      state.mallData = data;
    },
  },
  actions: {
    //用户登录
    async Login({ commit }, data) {
      const time = getDate(data.time);
      const user = { ...data, time };
      localStorage.setItem("userInfo", JSON.stringify(user));
      commit("setUserInfo", user);
    },
    //获取首页商品数据
    async getHomeData({ commit }) {
      const res = await api.getHomeList();
      console.log(res);
      commit("setHomeData", res);
      localStorage.setItem("homeData", JSON.stringify(res));
    },
    //获取mall页面商品数据
    getMallData({ commit }, data) {
      commit("setMallData", data);
      localStorage.setItem("mallData", JSON.stringify(data));
    },
  },
  getters: {
    userInfo: (state) => state.userInfo,
  },
};
